var numSave = 0;
$(function(){
	$.post(rootPath+"/resource/getResourceList",{},function(data){
		var str = "";
		for(var i =0;i<data.length;i++){
			var system = data[i];
			str+='<tr class="branch collapsed" ztreeRow="system'+i+'" style="display: table-row;">'+
			'<td width="25%" class="firstMenu">'+
				'<div style="float:left;">'+
					'<span style="padding-left:0px;" class="indenter">'+
						'<a href="javascript:void(0)" class="current">&nbsp;</a>'+
					'</span><img src="'+rootPath+'/statics/images/icon_folder2.png" />'+
					'<span>'+system.systemName+'</span>'+
				'</div>'+
			'</td>'+
			'<td  class="">'+
				'<span>系统</span>'+
			'</td>'+
	        '<td>-</td>'+
			'<td class="">'+
			'	-'+
			'</td>'+
	        '<td class="">'+
			'	-'+
			'</td>'+
		'</tr>';
			str += showResourceList(system.resourceList,0,"system"+i);
		}
		$("#resourceTbody").html(str);
		$(".indenter").click(function(){
			var indenter = $(this);
			var ztreeRow = $(this).parent().parent().parent().attr("ztreerow");
			$("tr").each(function(){
				var thisRow = $(this).attr("ztreerow");
				if(thisRow != null && thisRow != undefined && thisRow != ""){
					if(thisRow != ztreeRow){
						if(thisRow.indexOf(ztreeRow) >=0){
							if($(indenter).children("a").attr("class") == "current"){
								$(this).attr("style","display:table-row;");
							}else{
								$(this).attr("style","display:none;");		
							}
						}
					}else{
						if($(indenter).children("a").attr("class") == "current"){
							$(indenter).children("a").removeClass("current");
						}else{
							$(indenter).children("a").addClass("current");
						}
					}
				}
			});
		});
		
		$(".edit_resource").click(function(){
			toEdit($(this));
		});
		$(".edit_status").click(function(){
			updateStatu($(this));
		});
		$("#systemId").change(function(){
			 addSystem();
		});
	});
	
	
});

function showResourceList(resourceList,num,classId){
	num++;
	var left = 19*num;
	var str = "";
	if(resourceList != null){
		for(var i =0;i<resourceList.length;i++){
			var resource = resourceList[i];
			var className = classId+"_code"+i;
			str += '<tr class="branch collapsed"  ztreeRow="'+className+'" style="">'+
						'<td class="">'+
						'<div style="float:left;">'+
							'<span style="padding-left:'+left+'px;" class="indenter">'+
								'<a href="javascript:void(0)" class="current">&nbsp;</a>'+
							'</span><img src="'+rootPath+'/statics/images/icon_folder2.png" />'+
							'<span>'+resource.resourceName+'</span>'+
						'</div>'+
					'</td>'+
					'<td  class="">'+
						'<span >'+resource.resourceType +'</span>'+
					'</td>'+
			        '<td>'+resource.sortNo +'</td>'+
			        '<td>';
			if(resource.status == '0'){
				str+= '<span class="green">启用</span>';
			}else{
				str+= '<span class="red">停用</span>';
			}
			    str+= ' </td>'+
					'<td  class="">'+
						'<a href="javascript:void(0)" class="edit_resource" resource="'+resource.id+'" >编辑</a>';
			if(resource.status == '0'){
				str+= '<a href="javascript:void(0)" class="tybtn edit_status" resource="'+resource.id+'" resourcestatus="1">停用</a>';
			}else{
				str+= '<a href="javascript:void(0)" class="tybtn edit_status" resource="'+resource.id+'" resourcestatus="0">启用</a>';
			}
						
				str+=	'</td>'+
				'</tr>';
		    str += showResourceList(resource.resourceChildList,num,className);
		}
		return str;
	}else{
		return str;
	}
}

/**
 * 跳转添加页面
 */
function toAddResource(){
	location.href = rootPath+"/resource/toAddResource";
}

/**
 * @param systemId
 */
function addSystem(){
	$("#parentName").html("请选择");
	var systemId = $("#systemId").val();
	var nodes = []; 
	$.ajax({
		url : rootPath + "/resource/getResourceBySystem",
		async : false,
		dataType : 'json',
		data:{systemId:systemId},
		type : 'post',
		success : function(data) {
			if (data != null) {
				$.each(data, function(index, row) {
					nodes.push({
						id : row.id,
						parentId : row.parentId,
						name : row.resourceName
					});
				});
			}
		},
		fail : function() {
			alert("fail!!!!!!!");
		}
	});

	/**
	 * zTree设置
	 */
	var setting = {
		data : {
			simpleData : {
				enable : true,
				idKey : 'id',
				pIdKey : 'parentId',
				rootPId : "0"
			}
		},
		callback : {
			onClick : onClick
		}
	};
	var treeObj = $.fn.zTree.init($("#zTree"), setting, nodes);
	treeObj.expandAll(true);
}

function onClick(e, treeId, treeNode) {
	// 判断div是否影藏状态
	var isHidden = $("#orgInfo").is(":hidden");
	if (isHidden) {
		$("#orgInfo").show();
	}
	var zTree = $.fn.zTree.getZTreeObj("zTree"), nodes = zTree
			.getSelectedNodes(), name = "", id = "", parentId = "";
	nodes.sort(function compare(a, b) {
		return a.id - b.id;
	});
	for (var i = 0, l = nodes.length; i < l; i++) {
		name += nodes[i].name + ",";
		id += nodes[i].id + ",";
		parentId += nodes[i].parentId + ",";
	}
	if (id.length > 0)
		id = id.substring(0, id.length - 1);
	if (name.length > 0)
		name = name.substring(0, name.length - 1);
	if (parentId.length > 0)
		parentId = parentId.substring(0, parentId.length - 1);

	$("#parentName").html(name);
	$("#parentId").val(id);
}

/**
 * 保存
 */
function resourceAdd(){
	if(numSave==0){
		numSave+=1;
		$.post(rootPath+"/resource/addResource",$('#resourceForm').serialize(),function(data){
			if(data>0){
				$(".tsContext").html("添加成功!");
				$('.res_divk').toggle();
				$('#zy_box').toggle();
				setTimeout("getList()",1000);
			}else{
				$(".tsContext").html("添加失败!");
				$('#zy_box').toggle();
				$('.res_divk').toggle();
				numSave=0;
			}
		});
	}
}

function getList(){
	location.href = rootPath +"/page/resource/resource_list.jsp";
}
$(function(){
	$("#save_btn").click(function(){
		resourceAdd();
	});
	$("#getList").click(function(){
		getList();
	});
	
	$("#ztreedl").click(function(){
		var className= $(this).attr("class");
		if(className == "hover"){
			$(this).attr("class","");
		}else{
			$(this).attr("class","hover");
		}
		
	});
	$("#zTree").mouseleave(function(){
		$("#ztreedl").attr("class","");
	});
});

/**
 * 跳转修改
 */
function toEdit(object){
	var id = $(object).attr("resource");
	location.href = rootPath+"/resource/toEditResource?id="+id;
}

function updateStatu(object){
	var id = $(object).attr("resource");
	var status = $(object).attr("resourcestatus");
	$.post(rootPath+"/resource/updateResourceStatus",{id:id,status:status},function(data){
		if(data>0){
			if(status== 0){
				$(".tsContext").html("启用成功!");
			}else{
				$(".tsContext").html("停用成功!");
			}			
			$('.res_divk').toggle();
			$('#zy_box').toggle();
			setTimeout("getList()",1000);
		}else{
			if(status== 0){
				$(".tsContext").html("启用失败!");
			}else{
				$(".tsContext").html("停用失败!");
			}
			
			$('#zy_box').toggle();
			$('.res_divk').toggle();
			numSave=0;
		}
	});
}